package admin

import (
	"gf/app/model"
	"gf/app/service"
	"gf/library/response"
	"github.com/gogf/gf/net/ghttp"
)

// 用户API管理对象
var User = userApi{}

type userApi struct{
	Token string
}

func (a *userApi) Login(r *ghttp.Request) {

	var (
		data *model.UserApiSignInReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	token, err := service.User.SignIn(r.Context(), data.Username, data.Password)

	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	a.Token = token
	response.JsonExit(r, 0, "ok", a)

}


func (a *userApi) ResetPassword(r *ghttp.Request) {

	var (
		data *model.UserApiSignInReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	token, err := service.User.ResetPassword(r.Context(), data.Username, data.Password)

	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	a.Token = token
	response.JsonExit(r, 0, "ok", a)

}

